package com.sykj.why.mongo.service.impl;

import com.sykj.why.exception.WhyException;
import com.sykj.why.mongo.dao.ContentTDao;
import com.sykj.why.mongo.document.entity.ContentCategoryT;
import com.sykj.why.mongo.document.entity.ContentT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.AlbumTService;
import com.sykj.why.mongo.service.ContentCategoryTService;
import com.sykj.why.mongo.service.ContentConfigTService;
import com.sykj.why.mongo.service.ContentTService;
import com.sykj.why.mongo.vo.entity.ContentTVo;
import com.sykj.why.util.BaseTools;
import com.sykj.why.util.DateTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("ContentTService")
public class ContentTServiceImpl extends BasicServiceImpl<ContentT> implements ContentTService {

    @Autowired
    private ContentTDao contentTDao;

    @Autowired
    private AlbumTService albumTService;

    @Autowired
    private ContentCategoryTService contentCategoryTService;

    @Autowired
    private ContentConfigTService contentConfigTService;

    /**
     * 添加内容
     * @param contentT
     */
    @Override
    public void saveContent(ContentT contentT) {
        //排序
        long count = contentTDao.count(ContentT.class, false);
        contentT.setSort((int) count);
        //设置时间
        contentT.setCreateTime(DateTool.getSystemTime());
        contentT.setUpdateTime(DateTool.getSystemTime());
        if (StringUtils.isNoneEmpty(contentT.getName())){
            contentT.setName(contentT.getName());
        }else {
            contentT.setName("暂无文件名");
        }
        //保存
        contentTDao.save(contentT);
    }

    /**
     * 删除内容
     * @param id
     */
    @Override
    public void removeContent(String id) {
        //通过Id查询内容
        ContentT contentT = findById(id);
        //逻辑删除内容
        contentTDao.remove(contentT);
    }

    /**
     * 修改内容
     * @param contentT
     */
    @Override
    public void updateContent(ContentT contentT) {
        //通过id查询内容
        ContentT content = findById(contentT.getId());
        //重新赋值
        content.setContent(contentT.getContent());
        if (StringUtils.isNoneEmpty(contentT.getName())){
            content.setName(contentT.getName());
        }else {
            content.setName("暂无文件名");
        }
        content.setUpdateTime(DateTool.getSystemTime());
        //保存或修改
        contentTDao.saveOrupdate(content);
    }

    /**
     * 启用/禁用内容
     * @param id
     * @param flag
     */
    @Override
    public void disableOrEnable(String id, boolean flag) {
        //通过id查询内容
        ContentT contentT = findById(id);
        //修改状态
        contentTDao.disableOrEnable(contentT,flag);
    }

    /**
     * 通过id查询内容
     * @param id
     * @return
     */
    @Override
    public ContentT findById(String id) {
        //通过id查询未删除的内容
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id).and("removed").is(false));
        ContentT contentT = contentTDao.findOne(query,ContentT.class);
        if (contentT!=null){
            return contentT;
        }else {
            throw new WhyException(ResultEnum.OBJISNULL);
        }
    }

    /**
     * 查询所有内容
     * @return
     */
    @Override
    public List<ContentT> findAll() {
        //查询未删除和未禁用的所有内容
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).and("enable").is(true));
        List<ContentT> list = contentTDao.findAll(query,ContentT.class);
        return list;
    }

    /**
     * 查询专辑下的内容
     * @param albumId
     * @param categoryId
     * @return
     */
    @Override
    public List<ContentTVo> findByAlbum(String albumId,String categoryId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("removed").is(false).
                and("albumId").is(albumId));
        if (categoryId!=null){
            query.addCriteria(Criteria.where("contentCategoryId").is(categoryId));
        }
        query.with(new Sort(Sort.Direction.ASC, "sort"));
        List<ContentT> contentTList = contentTDao.findAll(query,ContentT.class);
        return vo(contentTList,albumId);
    }

    public List<ContentTVo> vo(List<ContentT> contentTList,String albumId){
        if (contentTList.size()>0){
            //增加专辑浏览次数
            albumTService.setViewCount(albumId);
            List<ContentTVo> list = new ArrayList<>();
            for (ContentT contentT : contentTList) {
                ContentTVo vo = new ContentTVo();
                BeanUtils.copyProperties(contentT, vo);
                if (contentT.getContentCategoryId()!=null){
                    ContentCategoryT contentCategoryT = contentCategoryTService.findById(contentT.getContentCategoryId());
                    vo.setCategoryName(contentCategoryT.getCategoryName());
                    vo.setUrl(contentCategoryT.getUrl());
                }
                vo.setCreateTime(BaseTools.longToString(contentT.getCreateTime()));
                vo.setUpdateTime(BaseTools.longToString(contentT.getUpdateTime()));
                list.add(vo);
            }
            return list;
        }else {
            return null;
        }
    }

}
